home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / shell / cdialog-.9a / cdialog- / cdialog-0.9a / inputbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-15  |  6.9 KB  |  252 lines

  1. /*
  2.  *  inputbox.c -- implements the input box
  3.  *
  4.  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5.  *
  6.  *  This program is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU General Public License
  8.  *  as published by the Free Software Foundation; either version 2
  9.  *  of the License, or (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include "dialog.h"
  22.  
  23. unsigned char dialog_input_result[MAX_LEN + 1];
  24.  
  25. /*
  26.  * Display a dialog box for inputing a string
  27.  */
  28. int
  29. dialog_inputbox (const char *title, const char *cprompt, int height, int width,
  30.          const char *init)
  31. {
  32.     int i, x, y, box_y, box_x, box_width;
  33.     int input_x = 0, scroll = 0, key = 0, button = -1;
  34.     unsigned char *instr = dialog_input_result;
  35.     WINDOW *dialog;
  36.     char *prompt=strclone(cprompt);
  37.  
  38.     tab_correct_str(prompt);
  39.     if (init != NULL) {
  40.       prompt=auto_size(prompt, &height, &width, 5, MIN(MAX(strlen(init)+7,26),SCOLS-(begin_set ? begin_x : 0)));
  41.     } else
  42.       prompt=auto_size(prompt, &height, &width, 5, 26);
  43.     print_size(height, width);
  44.     ctl_size(height, width);
  45.   
  46.     if (begin_set == 1) {
  47.       x = begin_x;
  48.       y = begin_y;
  49.     } else {
  50.       /* center dialog box on screen */
  51.       x = (SCOLS - width)/2;
  52.       y = (SLINES - height)/2;
  53.     }
  54.  
  55. #ifdef HAVE_NCURSES
  56.     if (use_shadow)
  57.     draw_shadow (stdscr, y, x, height, width);
  58. #endif
  59.     dialog = newwin (height, width, y, x);
  60.     mouse_setbase (x, y);
  61.     keypad (dialog, TRUE);
  62.  
  63.     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  64.     wattrset (dialog, border_attr);
  65.     wmove (dialog, height - 3, 0);
  66.     waddch (dialog, ACS_LTEE);
  67.     for (i = 0; i < width - 2; i++)
  68.     waddch (dialog, ACS_HLINE);
  69.     wattrset (dialog, dialog_attr);
  70.     waddch (dialog, ACS_RTEE);
  71.     wmove (dialog, height - 2, 1);
  72.     for (i = 0; i < width - 2; i++)
  73.     waddch (dialog, ' ');
  74.  
  75.     if (title != NULL) {
  76.     wattrset (dialog, title_attr);
  77.     wmove (dialog, 0, (width - strlen (title)) / 2 - 1);
  78.     waddch (dialog, ' ');
  79.     waddstr (dialog, title);
  80.     waddch (dialog, ' ');
  81.     }
  82.     wattrset (dialog, dialog_attr);
  83.     print_autowrap (dialog, prompt, width, 1, 2);
  84.  
  85.     /* Draw the input field box */
  86.     box_width = width - 6;
  87.     getyx (dialog, y, x);
  88.     box_y = y + 2;
  89.     box_x = (width - box_width) / 2;
  90.     mouse_mkregion (y + 1, box_x - 1, 3, box_width + 2, 'i');
  91.     draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2,
  92.           border_attr, dialog_attr);
  93.  
  94.     x = width / 2 - 11;
  95.     y = height - 2;
  96.     print_button (dialog, "Cancel", y, x + 14, FALSE);
  97.     print_button (dialog, "  OK  ", y, x, TRUE);
  98.  
  99.     /* Set up the initial value */
  100.     wmove (dialog, box_y, box_x);
  101.     wattrset (dialog, inputbox_attr);
  102.     if (!init)
  103.     instr[0] = '\0';
  104.     else
  105.     strcpy (instr, init);
  106.     input_x = strlen (instr);
  107.     if (input_x >= box_width) {
  108.     scroll = input_x - box_width + 1;
  109.     input_x = box_width - 1;
  110.     for (i = 0; i < box_width - 1; i++)
  111.         waddch (dialog, instr[scroll + i]);
  112.     } else
  113.     waddstr (dialog, instr);
  114.     wmove (dialog, box_y, box_x + input_x);
  115.  
  116.     wrefresh_lock(dialog);
  117.     wtimeout(dialog, WTIMEOUT_VAL);
  118.  
  119.     while (key != ESC) {
  120.     key = mouse_wgetch (dialog);
  121.  
  122.     if (button == -1) {    /* Input box selected */
  123.         switch (key) {
  124.         case TAB:
  125.         case KEY_UP:
  126.         case KEY_DOWN:
  127.         case M_EVENT + 'i':
  128.         case M_EVENT + 'o':
  129.         case M_EVENT + 'c':
  130.         break;
  131.         case KEY_LEFT:
  132.         continue;
  133.         case KEY_RIGHT:
  134.         continue;
  135.         case KEY_BACKSPACE:
  136.         case 127:
  137.         if (input_x || scroll) {
  138.             wattrset (dialog, inputbox_attr);
  139.             if (!input_x) {
  140.             scroll = scroll < box_width - 1 ?
  141.                 0 : scroll - (box_width - 1);
  142.             wmove (dialog, box_y, box_x);
  143.             for (i = 0; i < box_width; i++)
  144.                 waddch (dialog, instr[scroll + input_x + i] ?
  145.                     instr[scroll + input_x + i] : ' ');
  146.             input_x = strlen (instr) - scroll;
  147.             } else
  148.             input_x--;
  149.             instr[scroll + input_x] = '\0';
  150.             wmove (dialog, box_y, input_x + box_x);
  151.             waddch (dialog, ' ');
  152.             wmove (dialog, box_y, input_x + box_x);
  153.             wrefresh_lock (dialog);
  154.         }
  155.         continue;
  156.         default:
  157.         if (key < 0x100 && isprint (key)) {
  158.             if (scroll + input_x < MAX_LEN) {
  159.             wattrset (dialog, inputbox_attr);
  160.             instr[scroll + input_x] = key;
  161.             instr[scroll + input_x + 1] = '\0';
  162.             if (input_x == box_width - 1) {
  163.                 scroll++;
  164.                 wmove (dialog, box_y, box_x);
  165.                 for (i = 0; i < box_width - 1; i++)
  166.                 waddch (dialog, instr[scroll + i]);
  167.             } else {
  168.                 wmove (dialog, box_y, input_x++ + box_x);
  169.                 waddch (dialog, key);
  170.             }
  171.             wrefresh_lock (dialog);
  172.             } else
  173.             flash ();    /* Alarm user about overflow */
  174.             continue;
  175.         }
  176.         }
  177.     }
  178.     switch (key) {
  179.     case 'O':
  180.     case 'o':
  181.         delwin (dialog);
  182.         return 0;
  183.     case 'C':
  184.     case 'c':
  185.         delwin (dialog);
  186.         return 1;
  187.     case M_EVENT + 'i':    /* mouse enter events */
  188.     case M_EVENT + 'o':    /* use the code for 'UP' */
  189.     case M_EVENT + 'c':
  190.         button = (key == M_EVENT + 'o') - (key == M_EVENT + 'c');
  191.     case KEY_UP:
  192.     case KEY_LEFT:
  193.         switch (button) {
  194.         case -1:
  195.         button = 1;    /* Indicates "Cancel" button is selected */
  196.         print_button (dialog, "  OK  ", y, x, FALSE);
  197.         print_button (dialog, "Cancel", y, x + 14, TRUE);
  198.         wrefresh_lock (dialog);
  199.         break;
  200.         case 0:
  201.         button = -1;    /* Indicates input box is selected */
  202.         print_button (dialog, "Cancel", y, x + 14, FALSE);
  203.         print_button (dialog, "  OK  ", y, x, TRUE);
  204.         wmove (dialog, box_y, box_x + input_x);
  205.         wrefresh_lock (dialog);
  206.         break;
  207.         case 1:
  208.         button = 0;    /* Indicates "OK" button is selected */
  209.         print_button (dialog, "Cancel", y, x + 14, FALSE);
  210.         print_button (dialog, "  OK  ", y, x, TRUE);
  211.         wrefresh_lock (dialog);
  212.         break;
  213.         }
  214.         break;
  215.     case TAB:
  216.     case KEY_DOWN:
  217.     case KEY_RIGHT:
  218.         switch (button) {
  219.         case -1:
  220.         button = 0;    /* Indicates "OK" button is selected */
  221.         print_button (dialog, "Cancel", y, x + 14, FALSE);
  222.         print_button (dialog, "  OK  ", y, x, TRUE);
  223.         wrefresh_lock (dialog);
  224.         break;
  225.         case 0:
  226.         button = 1;    /* Indicates "Cancel" button is selected */
  227.         print_button (dialog, "  OK  ", y, x, FALSE);
  228.         print_button (dialog, "Cancel", y, x + 14, TRUE);
  229.         wrefresh_lock (dialog);
  230.         break;
  231.         case 1:
  232.         button = -1;    /* Indicates input box is selected */
  233.         print_button (dialog, "Cancel", y, x + 14, FALSE);
  234.         print_button (dialog, "  OK  ", y, x, TRUE);
  235.         wmove (dialog, box_y, box_x + input_x);
  236.         wrefresh_lock (dialog);
  237.         break;
  238.         }
  239.         break;
  240.     case ' ':
  241.     case '\n':
  242.         delwin (dialog);
  243.         return (button == -1 ? 0 : button);
  244.     case ESC:
  245.         break;
  246.     }
  247.     }
  248.  
  249.     delwin (dialog);
  250.     return -1;            /* ESC pressed */
  251. }
  252.